package com.my.server;

import com.my.handler.CommandManager;
import com.my.handler.HttpRpc;
import com.my.util.Threads;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.socket.ServerSocketChannelFactory;
import org.jboss.netty.channel.socket.nio.NioServerBossPool;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.channel.socket.nio.NioWorkerPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * @author Hujf
 * @title: RarnMain
 * @date 2021-10-25 9:15
 * @description: 启动类
 */
public class RarnMain {

    static Logger log = LoggerFactory.getLogger(RarnMain.class);

    private static NettyServer server;

    public static void main(String[] args) {
        ServerSocketChannelFactory serverSocketChannelFactory;

        int workers = Runtime.getRuntime().availableProcessors() * 2;

        //netty创建一个服务端  用一个线程池来处理连接
        Executor executor = Executors.newCachedThreadPool();
        NioServerBossPool bossPool = new NioServerBossPool(executor, 1, new Threads.WorkerThreadNamer());
        NioWorkerPool workerPool = new NioWorkerPool(executor, workers, new Threads.WorkerThreadNamer());

        serverSocketChannelFactory = new NioServerSocketChannelFactory(bossPool, workerPool);


        ServerBootstrap server = new ServerBootstrap(serverSocketChannelFactory);
        CommandManager manager = CommandManager.instance(server);


        server.setPipelineFactory(new PipelineFactory());

        InetSocketAddress address = new InetSocketAddress("127.0.0.1", 6666);
        server.bind(address);

        log.info(address.getAddress()+":"+address.getPort()+" server is started");
    }
}
